package main.test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @Author: 郑国荣
 * @Date: 2023-09-15-16:07
 * @Description:
 */
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class ModifyTextFile {
    public static void main(String[] args) {
        String filePath = "D:\\java_environment\\nginx-1.24.0\\static\\1.txt";
        StringBuilder modifiedContent = new StringBuilder();

        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "GBK"))) {
            String currentLine;
            String nextLine;

            while ((currentLine = br.readLine()) != null) {
                modifiedContent.append(currentLine);

                nextLine = br.readLine();
                if ( !nextLine.isEmpty() && nextLine.charAt(0) != ' ') { // 如果下一行不为空且不是以空格开头
                    // If the next line is not empty and doesn't start with a space, append it to the current line.
                    modifiedContent.append(nextLine);
                } else if (nextLine != null) {
                    // If the next line is empty or starts with a space, add a newline.
                    modifiedContent.append("\n");
                    modifiedContent.append(nextLine);
                }

                // Add a newline after processing each pair of lines.
                modifiedContent.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Print the modified content.
        System.out.println(modifiedContent.toString());

        // If you want to save the modified content back to the file, you can do so here.
        // You can use BufferedWriter to write the content back to the file.
    }
}

